package com.dcy.daily.controller;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.dcy.daily.common.core.base.BaseController;
import com.dcy.daily.common.core.domain.ResultVO;
import com.dcy.daily.common.core.page.TableDataInfo;
import com.dcy.daily.common.enums.ResponseCodeEnum;
import com.dcy.daily.common.exception.ValidateException;
import com.dcy.daily.core.mapper.GoodMapper;
import com.dcy.daily.core.model.Account;
import com.dcy.daily.core.model.Book;
import com.dcy.daily.core.model.Good;
import com.dcy.daily.service.service.AccountService;
import com.dcy.daily.service.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 项目初始化时，测试Controller
 *
 * @author duanchunyu
 * @version 1.0.0
 * @createTime 2021年06月15日 13:31:00
 */
@RestController
public class DemoController extends BaseController {

    @NacosValue(value = "${name:false}",autoRefreshed = true)
    private String name;

    @Autowired
    private AccountService accountService;

    @Autowired
    private GoodMapper goodMapper;
    /**
     * 使用redis才打开
     */
//    @Autowired
//    private RedissonService redissonService;

    @Autowired
    private DemoService demoService;

    /**
     * 查询用户
     * @param id
     * @return
     */
    @RequestMapping("/getAccountById")
    public Account getAccountById(@RequestParam("id") Integer id){
        Account account = accountService.getAccount(id);
        return account;
    }


    /**
     * 更新用户
     * @param account
     * @return
     */
    @RequestMapping("updateAccount")
    public Integer updateAccount(@RequestBody Account account){
        Integer updateAccount = accountService.updateAccount(account);
        return updateAccount;
    }

   /* @RequestMapping("/redis")
    public void redis(){
        redissonService.setValue("m","a");
    }

    @RequestMapping("/getRedis")
    public String getRedis(){
        Object t = redissonService.getValue("t");
        return (String)t;
    }*/

    @RequestMapping("/keyGen")
    public Book getBooks(@RequestParam("isbin") String isbin,@RequestParam("title") String title){
        Book byIsbn = demoService.getByIsbn(isbin,title);
        return byIsbn;
    }

    /**
     * 异常测试
     */
    @RequestMapping("/exception")
    public void exception(){
        throw new ValidateException(ResponseCodeEnum.OBJECT_TO_JSON_ERROR.getCode());
    }

    /**
     * 分页请求
     * @return
     */
    @RequestMapping("/handlePage")
    public ResultVO<TableDataInfo> handPage(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize){
        List<Account> accountPageInfo = accountService.handlePageAccount(pageNum, pageSize);
        return success(accountPageInfo,true);
    }

    /**
     * 增加商品，测试雪花Id算法
     * @param good
     * @return
     */
    @RequestMapping("/insertGood")
    public Integer  insertAccount(Good good){
        int ret = goodMapper.insert(good);
        return ret;
    }
}
